package com.uidp.ui;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import com.acsior.sbc.exception.BusinessException;
import com.acsior.sbc.exception.SearchException;
import com.acsior.wu.bean.MBGeneralEditableObject;
import com.acsior.wu.nom.UIListManager;
import com.uidp.domain.AttributeType;
import com.uidp.domain.Attributes;
import com.uidp.domain.ModulElements;
import com.uidp.uitils.ManagedBeans;

public class MBAttribute extends MBGeneralEditableObject<Attributes> {

	private List<ModulElements> allGropus = new ArrayList<ModulElements>();

	private List<AttributeType> allAttributeType = new ArrayList<AttributeType>();

	private List<String> selectedGroups = new ArrayList<String>();

	private List<String> selectedTypes = new ArrayList<String>();

	private String selectedGroup;

	private String selectedAttrType;

	public MBAttribute() {
		super(Attributes.class);
		try {
			this.allAttributeType = AttributeType.searchAttributes("");
			this.allGropus = ModulElements.searchModulElements("");
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void initData() {
		setOrdering(new Integer(0));
		setIsRequired(new Boolean(false));

	}

	@Override
	protected boolean isValid() {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("static-access")
	public String saveAttribute() {
		try {
			getGeneral().setAttributeType(
					new AttributeType().searchAttributes(getSelectedAttrType())
							.get(0));
			getGeneral().setModulElements(
					new ModulElements().searchModulElements(getSelectedGroup())
							.get(0));
			getGeneral().store();
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * Списък с всички типове на трибутите
	 * 
	 * @return List
	 */
	public List<SelectItem> getNomAttributeType() {

		List<AttributeType> r = getAllAttributeType();
		List<String> attrTypes = new ArrayList<String>();
		for (AttributeType s : getAllAttributeType()) {
			attrTypes.add(s.getAttributeName());
		}
		if (r != null && !r.isEmpty()) {
			return UIListManager.buildSelectItemsFromStrings(attrTypes);
		}

		return new ArrayList<SelectItem>();
	}

	/**
	 * Списък с всички групи
	 * 
	 * @return List
	 */
	public List<SelectItem> getNomModulElements() {

		List<ModulElements> r = getAllGropus();
		List<String> groups = new ArrayList<String>();
		for (ModulElements s : getAllGropus()) {
			groups.add(s.getElementname());
		}
		if (r != null && !r.isEmpty()) {
			return UIListManager.buildSelectItemsFromStrings(groups);
		}

		return new ArrayList<SelectItem>();
	}

	/**
	 * @see MBGeneral#getManagedProp()
	 */
	@Override
	protected ManagedBeans getManagedProp() {
		return ManagedBeans.attributes;
	}

	public String getUniqname() {
		return getGeneral().getUniqname();
	}

	public void setUniqname(String uniqname) {
		getGeneral().setUniqname(uniqname);
	}

	public String getAttributeName() {
		return getGeneral().getAttributeName();
	}

	public void setAttributeName(String attributeName) {
		getGeneral().setAttributeName(attributeName);
	}

	public boolean getIsRequired() {
		return getGeneral().getIsRequired();
	}

	public void setIsRequired(boolean isRequired) {
		getGeneral().setIsRequired(isRequired);
	}

	public List<ModulElements> getAllGropus() {
		return allGropus;
	}

	public void setAllGropus(List<ModulElements> allGropus) {
		this.allGropus = allGropus;
	}

	public List<AttributeType> getAllAttributeType() {
		return allAttributeType;
	}

	public void setAllAttributeType(List<AttributeType> allAttributeType) {
		this.allAttributeType = allAttributeType;
	}

	public List<String> getSelectedGroups() {
		return selectedGroups;
	}

	public void setSelectedGroups(List<String> selectedGroups) {
		this.selectedGroups = selectedGroups;
	}

	public List<String> getSelectedTypes() {
		return selectedTypes;
	}

	public void setSelectedTypes(List<String> selectedTypes) {
		this.selectedTypes = selectedTypes;
	}

	public String getSelectedGroup() {
		return selectedGroup;
	}

	public void setSelectedGroup(String selectedGroup) {
		this.selectedGroup = selectedGroup;
	}

	public String getSelectedAttrType() {
		return selectedAttrType;
	}

	public void setSelectedAttrType(String selectedAttrType) {
		this.selectedAttrType = selectedAttrType;
	}
	public int getOrdering() {
		return getGeneral().getOrdering();
	}

	public void setOrdering(int ordering) {
		getGeneral().setOrdering(ordering);
	}

}
